package deliciouse.biz.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import deliciouse.biz.FoodBiz;
import deliciouse.dao.CategoryDao;
import deliciouse.dao.FoodDao;
import deliciouse.entity.Category;
import deliciouse.entity.Food;
import deliciouse.util.MybatisUtil;

public class FoodBizImpl implements FoodBiz {

	@Override
	public List<Food> getAll() {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			FoodDao dao = sess.getMapper(FoodDao.class);
			List<Food> foodlist = dao.getAll();
			return foodlist;
		} finally {
			sess.close();
		}
	}

	@Override
	public Food fetchById(int id) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			FoodDao dao = sess.getMapper(FoodDao.class);
			Food food = dao.fetchById(id);
			return food;
		} finally {
			sess.close();
		}
	}

	@Override
	public List<Food> fetchByCondition(int cid, String title, int pageNum, int pageSize) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			FoodDao dao = sess.getMapper(FoodDao.class);
			List<Food> foodlist = dao.fetchByCondition(cid, title, (pageNum - 1) * pageSize, pageSize);
			return foodlist;
		} finally {
			sess.close();
		}
	}

	@Override
	public int getAllRows(int cid, String title) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			FoodDao dao = sess.getMapper(FoodDao.class);
			int rows = dao.getAllRows(cid, title);
			return rows;
		} finally {
			sess.close();
		}
	}

	@Override
	public boolean addfoodlist(Food food) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {

			FoodDao dao = sess.getMapper(FoodDao.class);
			dao.addfoodlist(food);
			sess.commit();
			return true;
		} finally {
			sess.close();
		}
	}

	@Override
	public boolean updatefoodlist(Food food) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {

			FoodDao dao = sess.getMapper(FoodDao.class);
			dao.updatefoodlist(food);
			sess.commit();
			return true;
		} finally {
			sess.close();
		}

	}
}